<?php

class MStudentSchedule extends CI_Model{

	public function __construct()
	{
		parent::__construct();
	}
	
	function addStudentSchedule()
	{
		$data = array(
			'semester' => db_clean($_POST['semester']),
			'shift_id' => db_clean($_POST['shift_id']),
			'group' => $_POST['group'],
			'batch' => db_clean($_POST['batch']),
			'year' => db_clean($_POST['year']),
			'uid' => db_clean($_SESSION['uid']),
			'activate' => db_clean($_POST['activate'])
		);
		if ($_FILES)
		{
			$config['upload_path'] = './images/studentschedule/';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size'] = '2048';
			$config['remove_spaces'] = true;
			$config['overwrite'] = false;
			$config['max_width']  = '0';
			$config['max_height']  = '0';
			$this->load->library('upload', $config);	
		
			if (strlen($_FILES['schedule_src']['name'])){
				if(!$this->upload->do_upload('schedule_src')){
					$this->upload->display_errors();
					exit();
				}
				$image = $this->upload->data();
			
				if ($image['file_name']){
					$data['schedule_src'] = $image['file_name'];
				}
			}
		}
		$this->db->insert('student_schedule', $data);
	}
	
	function getAllStudentSchedule()
	{
		$data = array();
		$this->db->select('student_schedule.*, shift.title');
		$this->db->from('student_schedule');
		$this->db->join('shift', 'student_schedule.shift_id = shift.shift_id');
	    $this->db->order_by("student_schedule_id", "desc");
	    $Q = $this->db->get();
	    if ($Q->num_rows() > 0){
	       	foreach ($Q->result_array() as $row){
	         	$data[] = $row;
	       	}
	    }
	    $Q->free_result();  
	    return $data;
	}
	
	function deleteStudentSchedule($id){
	 	$source = $this->db->where('student_schedule_id', id_clean($id))->get('student_schedule')->row();
	 	
	 	if($source->result_src != null){
		 	if (file_exists(FCPATH.'images/studentschedule/'.$source->result_src)) {
	    		unlink('images/studentschedule/'.$source->result_src);
	    	}else {
	    		$this->session->set_flashdata('error', 'can not delete image from directory.');
	    	}
	 	}	 	
	 	$this->db->where('student_schedule_id', id_clean($id));
		$this->db->delete('student_schedule');	
	}
	
	function getStudentSchedule($id)
	{
		$data = array();
	    $this->db->where('student_schedule_id',id_clean($id));
	    $this->db->where('uid',id_clean($_SESSION['uid']));
	    $this->db->limit(1);
	    $Q = $this->db->get('student_schedule');
	    if ($Q->num_rows() > 0){
	      $data = $Q->row_array();
	    }
	
	    $Q->free_result();    
	    return $data;
	}
	
	function updateStudentSchedule()
	{
		$data = array(
			'semester' => db_clean($_POST['semester']),
			'shift_id' => db_clean($_POST['shift_id']),
			'group' => $_POST['group'],
			'batch' => db_clean($_POST['batch']),
			'year' => db_clean($_POST['year']),
			'uid' => db_clean($_SESSION['uid']),
			'activate' => db_clean($_POST['activate'])
		);
		 if ($_FILES['schedule_src']['name'] != null || $_FILES['schedule_src']['name'] != "" ){
		 	$source = $this->db->where('student_schedule_id', id_clean($_POST['student_schedule_id']))->get('student_schedule')->row();
    		if (unlink('images/studentschedule/'.$source->result_src)) {
    			
    		}else {
    			$this->session->set_flashdata('error', 'can not delete image from directory.');
    		}
			$config['upload_path'] = './images/studentschedule/';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size'] = '2048';
			$config['remove_spaces'] = true;
			$config['overwrite'] = false;
			$config['max_width']  = '0';
			$config['max_height']  = '0';
			$this->load->library('upload', $config);	
		
			if (strlen($_FILES['schedule_src']['name'])){
				if(!$this->upload->do_upload('schedule_src')){
					$this->upload->display_errors();
					exit();
				}
				$image = $this->upload->data();
			
				if ($image['file_name']){
					$data['schedule_src'] = $image['file_name'];
				}
			}
		 }
	 	$this->db->where('student_schedule_id', id_clean($_POST['schedule_id']));
		$this->db->update('student_schedule', $data);	
	}
}

?>